<?php
/**
 * CakeCMS - 管理员设置
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class AdministratorController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'RequestHandler',
			'AdminLog' 
	);
	public $helpers = array (
			'Page' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', '管理员设置' );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'administrator.js' 
		) );
	}
	public function index() {
		$this->set ( 'title_for_layout', '管理员管理' );
		
		// 接收分页参数
		if (isset ( $this->params ['page'] ) && is_numeric ( $this->params ['page'] )) {
			if (floor ( $this->params ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "Admin" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		// 整理搜索条件
		$conditions = array ();
		
		$list = $this->Admin->GetAdminByList ( $conditions, $limit );
		
		if (! empty ( $list ['result'] )) {
			$this->loadModel ( "AdminRole" );
			foreach ( $list ['result'] as &$val ) {
				$Role_info = $this->AdminRole->GetAdminRoleByID ( $val ['Admin'] ['roleid'] );
				$val ['Admin'] ['rolename'] = $Role_info ['AdminRole'] ['rolename'];
				$val ['Admin'] ['lastlogintime'] = date ( "Y-m-d H:i:s", $val ['Admin'] ['lastlogintime'] );
			}
		}
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 添加管理员
	 */
	public function add() {
		if ($this->request->isPost ()) {
			
			$data ['Admin'] ['username'] = $this->data ['username'];
			if (empty ( $data ['Admin'] ['username'] )) {
				$this->flash ( '用户名不能为空!', '/Administrator/add/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['password'] = $this->data ['password'];
			if (empty ( $data ['Admin'] ['password'] )) {
				$this->flash ( '密码不能为空!', '/Administrator/add/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['pwdconfirm'] = $this->data ['pwdconfirm'];
			if (empty ( $data ['Admin'] ['pwdconfirm'] )) {
				$this->flash ( '确认密码不能为空!', '/Administrator/add/', 1250 );
				return false;
			}
			
			if ($data ['Admin'] ['password'] != $data ['Admin'] ['pwdconfirm']) {
				$this->flash ( '两次输入的密码不相同!', '/Administrator/add/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['email'] = $this->data ['email'];
			if (empty ( $data ['Admin'] ['email'] )) {
				$this->flash ( 'E-mail不能为空!', '/Administrator/add/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['realname'] = $this->data ['realname'];
			if (empty ( $data ['Admin'] ['realname'] )) {
				$this->flash ( '真实姓名不能为空!', '/Administrator/add/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['roleid'] = $this->data ['roleid'];
			if (empty ( $data ['Admin'] ['roleid'] )) {
				$this->flash ( '所属角色不能为空!', '/Administrator/add/', 1250 );
				return false;
			}
			
			$data ['Admin'] ['lastloginip'] = $this->RequestHandler->getClientIP ();
			$data ['Admin'] ['lang'] = 'zh-cn';
			$data ['Admin'] ['lastlogintime'] = time ();
			
			$this->loadModel ( "Admin" );
			if ($this->Admin->add ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '添加管理员:' . $data ['Admin'] ['username'] );
				
				$this->flash ( '添加管理员成功!', '/Administrator/', 1250 );
				return false;
			} else {
				$this->flash ( '添加管理员失败!', '/Administrator/add/', 1250 );
				return false;
			}
		} else {
			$this->loadModel ( "AdminRole" );
			$conditions = array ();
			$Rolelist = $this->AdminRole->GetAdminRoleByAll ( $conditions );
			
			$this->set ( 'Rolelist', $Rolelist );
		}
	}
	
	/**
	 * 编辑管理员
	 */
	public function edit() {
		if ($this->request->isPost ()) {
			$uid = $this->data ['uid'];
			
			if (! empty ( $this->data ['password'] ) && ! empty ( $this->data ['pwdconfirm'] )) {
				$password = $this->data ['password'];
				$pwdconfirm = $this->data ['pwdconfirm'];
				
				if ($password != $pwdconfirm) {
					$msg = array (
							'rtn_code' => - 1,
							'content' => '两次输入密码不一样!' 
					);
					echo json_encode ( $msg );
					exit ();
				}
				
				$data ['Admin'] ['password'] = md5 ( $password );
			}
			
			$data ['Admin'] ['roleid'] = $this->data ['roleid'];
			if (empty ( $data ['Admin'] ['roleid'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '所属角色不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			$data ['Admin'] ['email'] = $this->data ['email'];
			if (empty ( $data ['Admin'] ['email'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => 'E-mail不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			$data ['Admin'] ['realname'] = $this->data ['realname'];
			if (empty ( $data ['Admin'] ['realname'] )) {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '真实姓名不能为空!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
			
			$data ['Admin'] ['uid'] = $this->data ['uid'];
			
			$this->loadModel ( "Admin" );
			if ($this->Admin->edit ( $data )) {
				// 记录操作日志
				$this->AdminLog->addLog ( '编辑管理员,管理员uid:' . $uid );
				
				$msg = array (
						'rtn_code' => 0,
						'content' => '编辑管理员成功!' 
				);
				echo json_encode ( $msg );
				exit ();
			} else {
				$msg = array (
						'rtn_code' => - 1,
						'content' => '编辑管理员失败!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
		} else {
			$uid = $this->params ['uid'];
			
			$this->loadModel ( "Admin" );
			$AdminInfo = $this->Admin->GetAdminByID ( $uid );
			
			$this->loadModel ( "AdminRole" );
			$conditions = array ();
			$Rolelist = $this->AdminRole->GetAdminRoleByAll ( $conditions );
			
			$this->set ( 'Rolelist', $Rolelist );
			$this->set ( 'AdminInfo', $AdminInfo );
		}
	}
	
	/**
	 * 删除管理员
	 */
	public function delete() {
		$uid = $this->data ['uid'];
		
		if (empty ( $uid )) {
			$msg = array (
					'rtn_code' => - 1,
					'content' => '删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
		
		$this->loadModel ( "Admin" );
		if ($this->Admin->del ( $uid )) {
			
			// 记录操作日志
			$this->AdminLog->addLog ( '删除管理员,管理员uid:' . $uid );
			
			$msg = array (
					'rtn_code' => 0,
					'content' => '删除成功!' 
			);
			echo json_encode ( $msg );
			exit ();
		} else {
			$msg = array (
					'rtn_code' => 0,
					'content' => '系统错误,删除失败!' 
			);
			echo json_encode ( $msg );
			exit ();
		}
	}
}
?>